/**Transcore is a remote-GUI to Transmission.
Copyright (C) 2009 by Jerry de Swart

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package transcore;

import java.io.IOException;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author J de Swart (Custombuild@gmail.com)
 */
public class DaemonSettings {

	private String encryption;
	private String downloadDir;
	private int speedLimitDown;
	private int speedLimitUp;
	private int peerLimit;
	private int port;
	private Boolean useSpeedLimitDown;
	private Boolean useSpeedLimitUp;
	private Boolean pexAllowed;
	private Boolean usePortForwarding;
	private String transVersion;

	public DaemonSettings() {
	}

	public void update(TransConnection conn) throws IOException {
		JSONMessage updateMessage = new JSONMessage(TransJSON.SESS_GET);

		try {
			JSONObject response = conn.send(updateMessage);

			JSONObject args = response.getJSONObject(TransJSON.ARGS);

			encryption = args.getString(TransJSON.ENCRYPT);
			downloadDir = args.getString(TransJSON.DOWN_DIR);
			speedLimitDown = args.getInt(TransJSON.DOWN_LIMIT);
			speedLimitUp = args.getInt(TransJSON.UP_LIMIT);
			peerLimit = args.getInt(TransJSON.PEER_LIMIT);
			port = args.getInt(TransJSON.PORT);
			useSpeedLimitDown = TransJSON.toBool(args.getInt(TransJSON.USE_DOWN_LIMIT));
			useSpeedLimitUp = TransJSON.toBool(args.getInt(TransJSON.USE_UP_LIMIT));
			pexAllowed = TransJSON.toBool(args.getInt(TransJSON.PEX_ALLOWED));
			usePortForwarding = TransJSON.toBool(args.getInt(TransJSON.USE_PORT_FORW));
			transVersion = args.getString(TransJSON.VERSION);
		} catch (JSONException e) {
			//TODO: Should be debug-error
			System.out.println(e.getMessage());
		}
	}

	@Deprecated
	public HashMap<String, Object> getDaemonSettings() {
		HashMap daemonSettings = new HashMap<String, Object>();

		//TODO: Should be TransJSON strings.. instead of static stuff
		daemonSettings.put("encryption", new String(encryption));
		daemonSettings.put("downloadDir", new String(downloadDir));
		daemonSettings.put("speedLimitDown", new Integer(speedLimitDown));
		daemonSettings.put("speedLimitUp", new Integer(speedLimitUp));
		daemonSettings.put("peerLimit", new Integer(peerLimit));
		daemonSettings.put("port", new Integer(port));
		daemonSettings.put("useSpeedLimitDown", new Boolean(useSpeedLimitDown));
		daemonSettings.put("useSpeedLimitUp", new Boolean(useSpeedLimitUp));
		daemonSettings.put("pexAllowed", new Boolean(pexAllowed));
		daemonSettings.put("usePortForwarding", new Boolean(usePortForwarding));
		daemonSettings.put("transVersion", new String(transVersion));

		return daemonSettings;
	}

	//TODO: Set this to use some map that sets the arguments
	public void setDaemonSettings(TransConnection conn, String encryption, String downloadDir, int speedLimitDown, int speedLimitUp, int peerLimit, int port, Boolean useSpeedLimitDown, Boolean useSpeedLimitUp, Boolean pexAllowed, Boolean usePortForwarding) throws IOException {
		JSONMessage setSettingsMessage = new JSONMessage(TransJSON.SESS_SET);

		HashMap<String, Object> settings = new HashMap<String, Object>();
		settings.put(TransJSON.ENCRYPT, encryption);
		settings.put(TransJSON.DOWN_DIR, downloadDir);
		settings.put(TransJSON.DOWN_LIMIT, speedLimitDown);
		settings.put(TransJSON.UP_LIMIT, speedLimitUp);
		settings.put(TransJSON.PEER_LIMIT, peerLimit);
		settings.put(TransJSON.PORT, port);
		settings.put(TransJSON.USE_DOWN_LIMIT, TransJSON.toInt(useSpeedLimitDown));
		settings.put(TransJSON.USE_UP_LIMIT, TransJSON.toInt(useSpeedLimitUp));
		settings.put(TransJSON.PEX_ALLOWED, TransJSON.toInt(pexAllowed));
		settings.put(TransJSON.USE_PORT_FORW, TransJSON.toInt(usePortForwarding));
		setSettingsMessage.setPairParams(settings);

		try {
			JSONObject returnObject = conn.send(setSettingsMessage);
			System.out.println("Returnmessage: " + returnObject.toString());
		} catch (JSONException e) {
			//TODO: Should be debug-error
			System.out.println(e.getMessage());
		}
	}

	@Override
	public String toString() {
		String returnString = "Encryption: " + encryption + "\n";
		returnString += "Download-dir: " + downloadDir + "\n";
		returnString += "Downloadspeed limit: " + speedLimitDown + "\n";
		returnString += "Uploadspeed limit: " + speedLimitUp + "\n";
		returnString += "Global peerlimit: " + peerLimit + "\n";
		returnString += "Port: " + port + "\n";
		returnString += "Use downspeed limit: " + useSpeedLimitDown + "\n";
		returnString += "Use upspeed limit: " + useSpeedLimitUp + "\n";
		returnString += "PEX allowed: " + pexAllowed + "\n";
		returnString += "Use port forwarding: " + usePortForwarding + "\n";
		returnString += "Transmission version: " + transVersion + "\n";
		return returnString;
	}

	public String getEncryption(){
		return encryption;
	}

	public String getDownloadDir(){
		return downloadDir;
	}

	public int getSpeedLimitDown(){
		return speedLimitDown;
	}

	public int getSpeedLimitUp(){
		return speedLimitUp;
	}

	public int getPeerLimit(){
		return peerLimit;
	}

	public int getPort(){
		return port;
	}

	public Boolean getUseSpeedLimitDown(){
		return useSpeedLimitDown;
	}

	public Boolean getUseSpeedLimitUp(){
		return useSpeedLimitUp;
	}

	public Boolean getPexAllowed(){
		return pexAllowed;
	}

	public Boolean getUsePortForwarding(){
		return usePortForwarding;
	}

	public String getTransVersion(){
		return transVersion;
	}
}
